package com.android.support.lib.universal.util;

import com.android.support.lib.universal.base.BaseApplication;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.widget.Toast;

/**
 * 判断网络是否连接
 *
 * @author 彭治铭
 */
public class Utilnet {
    private static Context context = BaseApplication.getInstance();
    private static ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    //判断是否连接到网络,无论是WiFi还是数据网络都有效。
    public static Boolean BNet() {
        Boolean bool = false;
        if (connectivity == null) {
            bool = false;
        } else {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info == null) {// 网络已断开
                bool = false;
            } else {
                if (info.isAvailable()) {// 网络已打开
                    bool = true;
                } else {
                    bool = false;
                }
            }
        }
        if (bool) {
            return bool;
        }
        Utilview.showToastView("亲，网络连接异常哦！");
        return bool;
    }

    //判断是否连接到Wifi网络，如果连接到返回当前连接的Wif，如果没有则返回NULL
    public static String BWifi() {
        NetworkInfo networkInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (networkInfo.isConnected()) {
            return wifiInfo.getSSID();
        }
        //Utilview.showToastView("亲，WIFI网络连接异常哦！");
        return null;
    }
}
